﻿//Author: Christian TQ
//License: MIT License
using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System.IO;

namespace TQ.Sharepoint.Webparts
{
    class MicrobloggerProcess
    {

        public static UserProfile GetCurrentUserProfile()
        {
            UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site));
            UserProfile userProfile = userProfileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
            return userProfile;
        }



        public static string DecodeHtml(string post, Microblogger.MicrobloggerUserControl uc)
        {
            string[] msgArray;
            string msg=string.Empty;
            
            StringWriter sw = new StringWriter();
            uc.Server.HtmlEncode(post, sw);
            msgArray = sw.ToString().Split(' ');
            foreach (string s in msgArray) {
                if (s.Contains("http://") || s.Contains("www."))              
                    msg += s.ToLink();
                else 
                    msg += ' ' + s + ' ';          
            }

            return msg;
        }


        public static string GetProfilePic(Colleague colleague, UserProfile userprofile)
        {
            string propertyName = "PictureUrl";
            string noPic = "/_layouts/images/O14_person_placeHolder_32.png";
            string profileThumb = string.Empty;

            ProfileValueCollectionBase profileValueCollection;
            profileValueCollection = (colleague != null) ? colleague.Profile.GetProfileValueCollection(propertyName) :
                                                          userprofile.GetProfileValueCollection(propertyName);
            return ((profileValueCollection.Value != null)) ? ((string)profileValueCollection.Value).ResizeThumb() : noPic;

        }

    }
}
